(() => {

    'use strict';
    goog.provide('Blockly.Arduino.ruilong');
    goog.require('Blockly.Arduino');

    

    Blockly.Arduino.forBlock['ruilong_resistor_scratch'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var code = 'analogRead(' + dropdown_pin + ')';
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock['ruilong_temp'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var code = 'analogRead(' + dropdown_pin + ')*0.488';
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock.ruilong_temp2 = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_fire = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_sound = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_moisture = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_steam = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_linefinder = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_knob = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_slider = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_joystick_x = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_joystick_y = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_turbidity = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_light = Blockly.Arduino.forBlock.ruilong_resistor_scratch;
    Blockly.Arduino.forBlock.ruilong_mqx = Blockly.Arduino.forBlock.ruilong_resistor_scratch;


    Blockly.Arduino.forBlock['ruilong_5button'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        //Blockly.Arduino.setups_['setup_input_'+dropdown_pin] = 'pinMode('+dropdown_pin+', INPUT);';
        var code = 'int Abutton(){\n'
            + 'int s;\n'
            + 'int a=analogRead(' + dropdown_pin + ');\n'
            + 'if(a<50) s=1;\n'
            + 'else if(a>100&&a<200) s=2;\n'
            + 'else if(a>200&&a<350) s=3;\n'
            + 'else if(a>350&&a<550) s=4;\n'
            + 'else if(a>550&&a<850) s=5;\n'
            + 'else  s=0;\n'
            + 'return s;'
            + '}\n';

        Blockly.Arduino.definitions_['Abutton'] = code;

        var code2 = 'Abutton()';

        return [code2, Blockly.Arduino.ORDER_ATOMIC];
    };
    Blockly.Arduino.forBlock.ruilong_minijoystick = Blockly.Arduino.forBlock.ruilong_5button;

    
    //ADS1015模数转换模块 设置范围以及精度
    Blockly.Arduino.forBlock['ruilong_ADS1015_setGain'] = function () {
        var ADS1X15 = this.getFieldValue('ADS1X15');
        var GAIN = this.getFieldValue('ADS1015_setGain');
        Blockly.Arduino.definitions_['include_Wire'+ADS1X15] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_Adafruit_ADS1015'+ADS1X15] = '#include <Adafruit_ADS1015.h>';
        Blockly.Arduino.definitions_['Adafruit_ADS1015_ads'+ADS1X15] = 'Adafruit_'+ADS1X15+' ads;\n';

        if (!Blockly.Arduino.setups_['setup_ads.begin()'+ADS1X15]) { Blockly.Arduino.setups_['setup_ads.begin()'+ADS1X15] = 'ads.begin(); \n'; }
        Blockly.Arduino.setups_['setup_ads.setGain'] = 'ads.setGain(' + GAIN + ');';
        var code = '';

        return code;
    };

    //ADS1015模数转换模块 采集数值
    Blockly.Arduino.forBlock['ruilong_ADS1015'] = function () {
        var ADS1X15 = this.getFieldValue('ADS1X15');
        Blockly.Arduino.definitions_['include_Wire'+ADS1X15] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_Adafruit_ADS1015'+ADS1X15] = '#include <Adafruit_ADS1015.h>';
        Blockly.Arduino.definitions_['Adafruit_ADS1015_ads'+ADS1X15] = 'Adafruit_'+ADS1X15+' ads;\n';
        if (!Blockly.Arduino.setups_['setup_ads.begin()'+ADS1X15]) {
            Blockly.Arduino.setups_['setup_ads.begin()'+ADS1X15] = 'ads.begin(); \n';
        }
        var dropdown_type = this.getFieldValue('ADS1015_AIN');
        var code = dropdown_type;
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    //PCF8591T模数转换模块 采集数值
    Blockly.Arduino.forBlock['ruilong_PCF8591T'] = function () {
        //Blockly.Arduino.definitions_['include_Wire'] = '#include "Arduino.h"';
        Blockly.Arduino.definitions_['include_PCF8591_h'] = '#include "PCF8591.h"';
        Blockly.Arduino.definitions_['#define_PCF8591_I2C_ADDRESS'] = '#define PCF8591_I2C_ADDRESS 0x48';
        Blockly.Arduino.definitions_['PCF8591'] = 'PCF8591 pcf8591(PCF8591_I2C_ADDRESS);';


        if (!Blockly.Arduino.setups_['setup_pcf8591.begin()']) {
            Blockly.Arduino.setups_['setup_pcf8591.begin()'] = 'pcf8591.begin(); \n';
        }
        var dropdown_type = this.getFieldValue('PCF8591T_AIN');
        var code = dropdown_type;
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };


   

})();